package face.pay.web.withdraw.service.impl;

import face.pay.constant.UserBizType;
import face.pay.dao.UserBankInfoDao;
import face.pay.entity.UserBankInfo;
import face.pay.entity.UserInfo;
import face.pay.redis.RedisUtils;
import face.pay.util.ResultMessage;
import face.pay.web.withdraw.service.IUserBankInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserBankInfoService implements IUserBankInfoService {


    @Autowired
    private UserBankInfoDao UserBankInfoDao;


    @Autowired
    RedisUtils redisUtils;


    /**
     * 查询用户提现信息
     *
     * @param loginUser
     * @return
     */
    @Override
    public UserBankInfo queryUserBankInfo(UserInfo loginUser) {
        UserBankInfo record = new UserBankInfo();
        record.setUserId(loginUser.getId());
        UserBankInfo config = this.UserBankInfoDao.queryUserBankInfo(record);
        if (config == null) {
            UserBankInfo userBankInfo = new UserBankInfo();
            userBankInfo.setUserId(loginUser.getId());
            userBankInfo.setOemId(loginUser.getOemId());
            userBankInfo.setCreatedBy(loginUser.getId());
            this.UserBankInfoDao.insert(userBankInfo);
            return userBankInfo;
        } else {
            return config;
        }
    }

    /**
     * 更改用户提现信息
     *
     * @param userBankInfo
     * @param loginUser
     * @param validateCode
     * @return
     */
    @Override
    public ResultMessage updateUserBankInfo(UserBankInfo userBankInfo, UserInfo loginUser, String validateCode) {
        //校验验证码
        String codeKey = UserBizType.USER_INFO_MODIFY + ":" + loginUser.getId();
        String redisCode = (String) redisUtils.get(codeKey);
        if (redisCode == null || !redisCode.equalsIgnoreCase(validateCode)) {
            return ResultMessage.createFail("验证码校验不正确");
        }
        redisUtils.delete(codeKey);

        userBankInfo.setUpdatedBy(loginUser.getId());
        UserBankInfoDao.update(userBankInfo);

        return ResultMessage.createSuccess();
    }
}
